import { computed } from 'vue';
import { Option } from './index.typing';

export const useForwardedSlots = (slots: any, prefix = '') => {
  return computed(() => {
    if (!prefix) {
      return slots;
    }

    // 获取所有以 "prefix-" 开头的插槽
    const slotNames = Object.keys(slots).filter((name) => name.startsWith(`${prefix}-`));

    // 创建一个新的对象来存储这些插槽
    const forwardedSlots: Record<string, any> = {};
    for (const name of slotNames) {
      forwardedSlots[name.replace(`${prefix}-`, '')] = slots[name];
    }

    return forwardedSlots;
  });
};

export const enumToOptions = (EnumObject: any): Option[] => {
  let result = EnumObject;
  if (typeof EnumObject === 'object' && !Array.isArray(EnumObject)) {
    result = Object.keys(EnumObject)
      .filter((key) => isNaN(Number(key))) // 过滤掉枚举的值部分，只保留键部分
      .map((key) => ({
        value: EnumObject[key],
        label: key,
      }));
  }
  return result;
};

export const mapToOptions = (MapObject: any): Option[] => {
  let result = MapObject;
  if (typeof MapObject === 'object' && !Array.isArray(MapObject)) {
    result = Object.keys(MapObject).map((key) => {
      return {
        value: key,
        label: MapObject[key],
      };
    });
  }
  return result;
};
